package com.linxiao.call.controller;

import com.linxiao.call.service.ICrmAgentMgrService;
import com.linxiao.call.vo.CrmAgentListResVO;
import com.linxiao.call.vo.CrmAgentParamReqVO;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * crm坐席管理
 *
 * @Author qiqi
 * @Date 2023/6/28 14:25
 * @Version 1.0
 */
@RestController
@RequestMapping("/crm/agentMgr")
public class CrmAgentMgrController extends BaseController {
    @Resource
    private ICrmAgentMgrService agentMgrService;
    @Resource
    private ISysUserService sysUserService;

    /**
     * 显示坐席列表
     *
     * @return
     */
    @RequestMapping("/showAgentList")
    public AjaxResult showAgentList(CrmAgentParamReqVO crmAgentParamReqVO) {
        List<CrmAgentListResVO> agentList = agentMgrService.showAgentList(crmAgentParamReqVO);
        if (StringUtils.isNotEmpty(agentList)) {
            // 根据坐席工号查询用户昵称
            List<String> agentIds = agentList.stream().filter(item -> StringUtils.isNotEmpty(item.getCno())).map(CrmAgentListResVO::getCno).collect(Collectors.toList());
            List<SysUser> sysUsers = sysUserService.selectUserListByAgentIds(agentIds);
            Map<String, String> agentMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getAgentId, u -> u.getNickName() != null ? u.getNickName() : "", (v1, v2) -> v1));
            agentList.forEach(item -> {
                item.setNickName(agentMap.get(item.getCno()));
            });
        }

        return AjaxResult.success(agentList);
    }

    /**
     * 坐席下线
     *
     * @param mchId
     * @param cno
     * @return
     */
    @RequestMapping("/onLine")
    @Log(title = "坐席下线", businessType = BusinessType.INSERT)
    public AjaxResult onLine(Long mchId, String cno) {
        if (mchId == null) {
            return AjaxResult.error("请求参数【mchId】 不能为空");
        }
        if (StringUtils.isEmpty(cno)) {
            return AjaxResult.error("请求参数【cno】 不能为空");
        }
        return agentMgrService.onLine(mchId, cno);
    }


}
